<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1 {
            width: 500px;
            height: 200px;
            background-color: red;
        }
        #box2 {
            width: 200px;
            height: 50px;
            background-color: yellowgreen;
        }
        #red {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
        }
    </style>
    <script type="text/javascript" >
        window.onload = ()=>{
            var box1 = document.getElementById("box1");
            let xyP = document.createElement("p");
            let box2 = document.getElementById("box2");
            box2.appendChild(xyP);

            box1.onmousemove = function(event){
                if(!event){
                    event = window.event;
                }
                event.cancelBubble = true; // 阻止事件冒泡
                console.log("x="+event.clientX);
                console.log("y="+event.clientY);
                box1.style.backgroundColor = "green";
                xyP.innerHTML = "x="+event.clientX+" y="+event.clientY;
            }
            // red 鼠标跟随
            let red = document.getElementById("red");
            window.onmousemove = function(event){
                if(!event){
                    event = window.event;
                }
                red.style.left = event.pageX+"px";
                red.style.top = event.pageY+"px";
                red.innerHTML = "x="+event.clientX+" y="+event.clientY;
            }
            

        }

    </script>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="red"></div>

</body>
</html>